package com.nice.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.nice.Commons;
import com.nice.cache.CacheMap;
import com.nice.price.AgtdPrice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class PriceController {
    private final static Logger logger = LoggerFactory.getLogger(PriceController.class);

    @Autowired
    private CacheMap cacheMap;
    /**
     * 跳转价格列表页面
     * @return
     */
    @GetMapping("/noble_metalPricehtml")
    public ModelAndView noble_metalPricehtml() {
        return new ModelAndView("noble_metalPrice.html");
    }

    /**
     * 跳转价格列表页面
     * @return
     */
    @GetMapping("/cny_all_pricehtml")
    public ModelAndView cny_all_pricehtml() {
        return new ModelAndView("cny_all_price.html");
    }

    /**
     * 跳转价格列表页面
     * @return
     */
    @RequestMapping("/priceSettingHtml")
    public ModelAndView priceSettingHtml(String name) {
        System.out.println(">>>name:"+name);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("name",name);
        modelAndView.getModel().put("name", name);
        modelAndView.setViewName("priceSetting.html");
        return modelAndView;
    }

    /**
     * post 方法获取白银价格
     * @return
     */
    @PostMapping("/noble_metal")
    @ResponseBody
    public JSONArray noble_metal() {
        List list= (List) cacheMap.get(Commons.NOBLE_METAL);
        return JSON.parseArray(JSON.toJSONString(list));
    }

    /**
     * post 方法获取CNY价格
     * @return
     */
    @PostMapping("/all_cny_price")
    @ResponseBody
    public JSONArray all_cny_price() {
        List list= (List) cacheMap.get(Commons.ALL_CNY);
        return JSON.parseArray(JSON.toJSONString(list));
    }

    /**
     * post 方法获取登录用户的邮箱
     * @return
     */
    @PostMapping("/queryUserEmail")
    @ResponseBody
    public String queryUserEmail(HttpSession session) {
        String getAttribute = String.valueOf(session.getAttribute(Commons.LOGIN_NAME));
        return getAttribute;
    }



    /**
     * get 方法获取白银价格
     * @return
     */
    @GetMapping("/getAgtdPrice")
    @ResponseBody
    public String getAgtdPrice() {
        AgtdPrice agtdPrice=new AgtdPrice();
        List list =agtdPrice.getAGTDPrice(true);
        return JSON.toJSONString(list);
    }

    @PostMapping("/getPrice")
    @ResponseBody
    public JSONArray getPrice(String name) {
        System.out.println(">>>name:"+name);
        List list= new ArrayList();
        if (Commons.AGTD.equals(name)) {
            list= (List) cacheMap.get(Commons.AGTD);
        }else if(Commons.AUTD.equals(name)) {
            list= (List) cacheMap.get(Commons.AUTD);
        }else{
            list= (List) cacheMap.get(name);

        }
        return JSON.parseArray(JSON.toJSONString(list));
    }
    /**
     * post 方法获取白银价格
     * @return
     */
    @PostMapping("/getAgtdPrice")
    @ResponseBody
    public JSONArray getAgtdPrice1() {
        AgtdPrice agtdPrice=new AgtdPrice();
//        List list =agtdPrice.getAGTDPrice();
        List list= (List) cacheMap.get(Commons.AGTD);
        return JSON.parseArray(JSON.toJSONString(list));
    }

    /**
     * post 方法获取白银价格
     * @return
     */
    @PostMapping("/getAutdPrice")
    @ResponseBody
    public JSONArray getAutdPrice() {
        AgtdPrice agtdPrice=new AgtdPrice();
//        List list =agtdPrice.getAGTDPrice();
        List list= (List) cacheMap.get(Commons.AUTD);
        return JSON.parseArray(JSON.toJSONString(list));
    }

    /**
     * 跳转白银价格列表页面
     * @return
     */
    @GetMapping("/agtdPricehtml")
    public ModelAndView agtdPricehtml() {
        return new ModelAndView("agtdPrice.html");
    }

    /**
     * 跳转价格列表页面
     * @return
     */
    @GetMapping("/userSetNotifyPricehtml")
    public ModelAndView userSetNotifyPricehtml() {
        return new ModelAndView("userSetNotifyPrice.html");
    }


    @PostMapping("/deleteAllUserInfo")
    @ResponseBody
    public String deleteAllUserInfo(HttpSession session) {
        logger.info("清空用户所有信息：deleteAllUserInfo");
        String email = String.valueOf(session.getAttribute(Commons.LOGIN_NAME));
        if(email!=null&&!"".equals(email)){
            this.cacheMap.getUserPriceSetMap().remove(email);
            logger.info("清空用户所有信息：UserPriceSetMap");

            //邮件的key是拼接的
            Map<String,String> map =this.cacheMap.getEmailMap();
            List<String> list = new ArrayList<String>();
            if (map != null) {
                for (String key : map.keySet()) {
                    list.add(key);
                    logger.info("清空用户所有信息：EmailMap{}",key);

                }
            }

            for (int i = 0; i < list.size(); i++) {
                String key = list.get(i);
                String[] strArray = key.split(":");
                String cacheEmail = strArray[0];
                if(cacheEmail.equals(email)){
                    map.remove(key);
                }
            }
            session.invalidate();
            session.removeAttribute(email);
            logger.info("清空用户所有信息：会话",email);

            return Commons.SUCCESS;
        }else{
            return null;
        }


    }
}
